package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.excel.ExcelOperUtil;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.P2PGrtzzqService;
import com.bringway.p2p.service.TxService;
import com.bringway.p2p.service.UserdzService;

import net.sf.json.JSONObject;

/*
 * @author fan
 * @className P2PGrtzzqController
 * @description 个人投资债权信息控制器
 * @date 2016年1月4日上午11:19:19
 */
@Controller
@RequestMapping("/grtzzq")
public class P2PGrtzzqController extends JsonController{
	@Autowired
	private P2PGrtzzqService p2pGrtzzqService;
	@Autowired
	private UserdzService userdzService;
	@Autowired
	private TxService txService;
	/**
	 * @方法名称:grtzzqQuery
	 * @方法描述:查询个人投资债权信息
	 * @author fan
	 * @date 2016年1月4日上午11:20:26
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryGrtzzq")
	public ModelAndView queryGrtzzq(HttpServletRequest request ,HttpServletResponse response)throws Exception{
		SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		JSONObject result = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer curpage = RequestUtils.getIntParameter(request, "start");
		Integer pageSize = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			curpage=(page-1)*pageSize;
		}
		String queryType = RequestUtils.getStringParameter(request, "queryType");
		String userId = RequestUtils.getStringParameter(request, "USERID");
		String TZSJ_START = RequestUtils.getStringParameter(request, "startDate");
		String TZSJ_END = RequestUtils.getStringParameter(request, "endDate");
		String TCZT = RequestUtils.getStringParameter(request, "TCZT");
		String HKZT = RequestUtils.getStringParameter(request, "HKZT");
		String SJHM = RequestUtils.getStringParameter(request, "SJHM");
		String TJR = RequestUtils.getStringParameter(request, "TJR");
		String TZ_SOURCE = RequestUtils.getStringParameter(request, "TZ_SOURCE");
		String XMMC = RequestUtils.getStringParameter(request, "XMMC");
		String HTBH = RequestUtils.getStringParameter(request, "HTBH");
		JSONObject parms = new JSONObject();
		parms.put("queryType", queryType);
		parms.put("userId", userId);
		parms.put("TZSJ_START", TZSJ_START);
		parms.put("TZSJ_END", TZSJ_END);
		parms.put("TCZT", TCZT);
		parms.put("HKZT", HKZT);
		parms.put("SJHM", SJHM);
		parms.put("TZ_SOURCE", TZ_SOURCE);
		parms.put("XMMC", XMMC);
		parms.put("TJR", TJR);
		parms.put("HTBH", HTBH);
		parms.put("curpage", curpage);
		parms.put("pageSize", pageSize);
		if(StringUtils.isNotEmpty(queryType) && "YWY_SSJY".equals(queryType)){//业务员所属客户查询
			parms.put("TJR", sysUserDetails.getGyh());//业务员所属客户交易查询-当前登陆客户作为推荐人
		}
		List<?>	list = p2pGrtzzqService.queryGrtzzq(parms);
		int	total = p2pGrtzzqService.getPageCount(parms);
		float sumJe = p2pGrtzzqService.getGrtzJe(parms);
		DecimalFormat df = new DecimalFormat("#,##0.00");
		result.put("data", list);
		result.put("total", total);
		result.put("sumJe", df.format(sumJe));
		if (null!=page) {//新版本,传参
			int totalPage=total%pageSize==0?total/pageSize:total/pageSize+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("USERID", "合计:");//key是字段名
			map.put("TCJE", df.format(sumJe));//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request, response, result);
		return null;
	}
	

	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping("exportTzrxxExcelMethod")
	public ModelAndView exportTzrxxExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String queryType = RequestUtils.getStringParameter(request, "queryType");
		String userId = RequestUtils.getStringParameter(request, "USERID");
		String TZSJ_START = RequestUtils.getStringParameter(request, "TZSJ_START");
		String TZSJ_END = RequestUtils.getStringParameter(request, "TZSJ_END");
		String TCZT = RequestUtils.getStringParameter(request, "TCZT");
		String HKZT = RequestUtils.getStringParameter(request, "HKZT");
		String SJHM = RequestUtils.getStringParameter(request, "SJHM");
		String TZ_SOURCE = RequestUtils.getStringParameter(request, "TZ_SOURCE");
		String XMMC = RequestUtils.getStringParameter(request, "XMMC");
		String TJR = RequestUtils.getStringParameter(request, "TJR");
		String HTBH = RequestUtils.getStringParameter(request, "HTBH");
		XMMC  = URLDecoder.decode(XMMC,"UTF-8");
		Integer curpage = RequestUtils.getIntParameter(request, "start");
		Integer pageSize = RequestUtils.getIntParameter(request, "limit");
		JSONObject parms = new JSONObject();
		parms.put("userId", userId);
		parms.put("TZSJ_START", TZSJ_START);
		parms.put("TZSJ_END", TZSJ_END);
		parms.put("TCZT", TCZT);
		parms.put("HKZT", HKZT);
		parms.put("SJHM", SJHM);
		parms.put("TZ_SOURCE", TZ_SOURCE);
		parms.put("XMMC", XMMC);
		parms.put("TJR", URLDecoder.decode(TJR));
		parms.put("HTBH", HTBH);
		parms.put("curpage", curpage);
		parms.put("pageSize", pageSize);
		if(StringUtils.isNotEmpty(queryType) && "YWY_SSJY".equals(queryType)){//业务员所属客户查询
			SysUserDetails sysUserDetails =	(SysUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			parms.put("TJR", sysUserDetails.getGyh());//业务员所属客户交易查询-当前登陆客户作为推荐人
		}
		// 查询数据
		List list = p2pGrtzzqService.queryGrtzzq(parms);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getTzrxxExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"个人投资信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "个人投资信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	
	/**
	 * @方法名称:youjiagrtzzq
	 * @方法描述:优家个人投资债权信息
	 * @author qjy
	 * @date 2016年1月4日上午11:20:26
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/youjiaGrtzzq")
	public ModelAndView youjiaGrtzzq(HttpServletRequest request ,HttpServletResponse response)throws Exception{
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer curpage = RequestUtils.getIntParameter(request, "start");
		Integer pageSize = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			curpage=(page-1)*pageSize;
		}
		String queryType = RequestUtils.getStringParameter(request, "queryType");
		String userId = RequestUtils.getStringParameter(request, "USERID");
		String TZSJ_START = RequestUtils.getStringParameter(request, "startDate");
		String TZSJ_END = RequestUtils.getStringParameter(request, "endDate");
		String TCZT = RequestUtils.getStringParameter(request, "TCZT");
		String HKZT = RequestUtils.getStringParameter(request, "HKZT");
		String SJHM = RequestUtils.getStringParameter(request, "SJHM");
		String TZ_SOURCE = RequestUtils.getStringParameter(request, "TZ_SOURCE");
		String XMMC = RequestUtils.getStringParameter(request, "XMMC");
		String TJR = RequestUtils.getStringParameter(request, "TJR");
		String Q_HTBH = RequestUtils.getStringParameter(request, "Q_HTBH");
		String Q_DQ = RequestUtils.getStringParameter(request, "Q_DQ");
		String Q_BM = RequestUtils.getStringParameter(request, "Q_BM");
		String Q_SS = RequestUtils.getStringParameter(request, "Q_SS");
		
		JSONObject parms = new JSONObject();
		parms.put("queryType", queryType);
		parms.put("userId", userId);
		parms.put("TZSJ_START", TZSJ_START);
		parms.put("TZSJ_END", TZSJ_END);
		parms.put("TCZT", TCZT);
		parms.put("HKZT", HKZT);
		parms.put("SJHM", SJHM);
		parms.put("TZ_SOURCE", TZ_SOURCE);
		parms.put("XMMC", XMMC);
		parms.put("TJR", TJR);
		parms.put("Q_HTBH", Q_HTBH);
		parms.put("Q_DQ", Q_DQ);
		parms.put("Q_BM", Q_BM);
		parms.put("Q_SS", Q_SS);
		parms.put("curpage", curpage);
		parms.put("pageSize", pageSize);
		JSONObject result = new JSONObject();
		if(StringUtils.isNotEmpty(TJR)){
			if(!TJR.startsWith("150")){
				serialize(request, response, result);
				return null;
			}
		}
		List list = p2pGrtzzqService.youjiaGrtzzq(parms);
		int count= p2pGrtzzqService.getyoujiaPageCount(parms);
		float zJe = p2pGrtzzqService.getyoujiaGrtzJe(parms);
		DecimalFormat cf = new DecimalFormat("#.00");
		result.put("data", list);
		result.put("total", count);
		result.put("sumJe", cf.format(zJe));
		if (null!=page) {//新版本,传参
			int totalPage=count%pageSize==0?count/pageSize:count/pageSize+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("USERID", "合计:");//key是字段名
			map.put("TCJE", cf.format(zJe));//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request, response, result);
		return null;
	}
	
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @return  导出excel表格
	 * @throws Exception
	 */
	@RequestMapping("exportyoujiaTzrxxExcelMethod")
	public ModelAndView exportyoujiaTzrxxExcelMethod(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String queryType = RequestUtils.getStringParameter(request, "queryType");
		String userId = RequestUtils.getStringParameter(request, "USERID");
		String TZSJ_START = RequestUtils.getStringParameter(request, "TZSJ_START");
		String TZSJ_END = RequestUtils.getStringParameter(request, "TZSJ_END");
		String TCZT = RequestUtils.getStringParameter(request, "TCZT");
		String HKZT = RequestUtils.getStringParameter(request, "HKZT");
		String SJHM = RequestUtils.getStringParameter(request, "SJHM");
		String TZ_SOURCE = RequestUtils.getStringParameter(request, "TZ_SOURCE");
		String XMMC = RequestUtils.getStringParameter(request, "XMMC");
		String TJR = RequestUtils.getStringParameter(request, "TJR");
		String Q_HTBH = RequestUtils.getStringParameter(request, "Q_HTBH");
		String Q_DQ = RequestUtils.getStringParameter(request, "Q_DQ");
		String Q_BM = RequestUtils.getStringParameter(request, "Q_BM");
		String Q_SS = RequestUtils.getStringParameter(request, "Q_SS");
		Q_DQ  = URLDecoder.decode(Q_DQ,"UTF-8");
		Q_BM  = URLDecoder.decode(Q_BM,"UTF-8");
		Q_SS  = URLDecoder.decode(Q_SS,"UTF-8");
		Integer curpage = RequestUtils.getIntParameter(request, "start");
		Integer pageSize = RequestUtils.getIntParameter(request, "limit");
		JSONObject parms = new JSONObject();
		parms.put("userId", userId);
		parms.put("TZSJ_START", TZSJ_START);
		parms.put("TZSJ_END", TZSJ_END);
		parms.put("TCZT", TCZT);
		parms.put("HKZT", HKZT);
		parms.put("SJHM", SJHM);
		parms.put("TZ_SOURCE", TZ_SOURCE);
		parms.put("XMMC", XMMC);
		parms.put("TJR", TJR);
		parms.put("Q_HTBH", Q_HTBH);
		parms.put("Q_DQ", Q_DQ);
		parms.put("Q_BM", Q_BM);
		parms.put("Q_SS", Q_SS);
		parms.put("curpage", curpage);
		parms.put("pageSize", pageSize);
		// 查询数据
		List list = p2pGrtzzqService.youjiaGrtzzq(parms);
		List<String[]> dataList = com.bringway.p2p.constant.ExcelHeadConstant.getTzrxxExportSearchResultData(list);
		
		// 导出数据到Excel文件
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		ExcelSheet sheet = new ExcelSheet();
		sheet.setBodyList(dataList);
		sheet.setSheetName(DateUtils.getCurrentDataStr()+"个人投资信息");
		sheetList.add(sheet);
		OutputStream os = response.getOutputStream();
		String fileName = DateUtils.getCurrentDataStr() + "个人投资信息.xls";
		super.setDownLoadResponseHeader(request, response, fileName);
		response.setContentType("application/vnd.ms-excel");
		
		// 导出EXCEL
		ExcelOperUtil.writeExcel(sheetList, os);
		return null;
	}
	@RequestMapping("/queryTzxxByGyh")
	public ModelAndView queryTzxxByGyh(HttpServletRequest request ,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String username = RequestUtils.getStringParameter(request, "gyh");
		List<Map<String, Object>> list = p2pGrtzzqService.queryTzxxByGyhList(username,start,limit);
		int	total = p2pGrtzzqService.getTzxxCountByGyh(username);
		float sumJe = p2pGrtzzqService.getTzxxSumJeByGyh(username);
		DecimalFormat df = new DecimalFormat("#,##0.00");
		result.put("data", list);
		result.put("total", total);
		result.put("sumJe", df.format(sumJe));
		if (null!=page) {//新版本,传参
			int totalPage=total%limit==0?total/limit:total/limit+1;
			result.put("totalPage", totalPage);
			//合计行 数据 开始
			Map<String, Object> map=new HashMap<>();
			map.put("USERID", "合计:");//key是字段名
			map.put("TZJE", df.format(sumJe));//key是字段名
			result.put("userdata", map);
			//合计行 数据 结束
		}
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/queryTzxxForm")
	public ModelAndView queryTzxxForm(HttpServletRequest request ,HttpServletResponse response)throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		String username = RequestUtils.getStringParameter(request, "gyh");
		params.put("gyh", username);
		params.put("start", 0);
		params.put("limit", 1);
		List<?> userdzInfo=userdzService.queryUserdz(params);
		result.put("userdzInfo", userdzInfo);
		params.put("txState", "00");
		String txdcl = txService.getTxSum(params);
		result.put("txdcl", txdcl);
		float ztzje = p2pGrtzzqService.getTzxxSumJeByGyh(username);
		DecimalFormat df = new DecimalFormat("#,##0.00");
		result.put("ztzje", df.format(ztzje));
		serialize(request, response, result);
		return null;
	}
}
